<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1000px" @open="open" @close="close" :modal-append-to-body="false" :append-to-body="true">
    <div style="padding:0 20px">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" disabled>
      <el-row>
        <el-col :span="12">
          <el-form-item label="督办单名称" prop="supervisename">
            <el-input v-model="model.supervisename"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="督办单类型" prop="supervisetype">
              <el-select v-model="model.supervisetype" filterable clearable @change="changesupervisetype">
                <el-option v-for="item in codeModel.supervisetypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="督办单编号" prop="supervisecode" v-show="false">
            <el-input v-model="model.supervisecode" :disabled="true" placeholder="系统自动赋值"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="督办对象大类" prop="superviseobjecttype">
            <el-select v-model="model.superviseobjecttype" filterable clearable @change="changesuperviseobjecttype" :disabled="!Boolean(model.supervisetype)" multiple>
                    <!-- <el-select v-model="model.superviseobjecttype" filterable clearable @change="changesuperviseobjecttype"> -->
                      <el-option v-for="item in computedBigtype" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="督办对象小类" prop="superviseobject">
            <el-input v-model="model.dispsuperviseobject" class="tl-ref-selector"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="责任人" prop="responsiblename">
            <tl-ref-user v-model="model.responsiblename" :initDisplayData.sync="model.responsiblename" dataType="code" :singleSelect="true" @ckick="userConfirm"></tl-ref-user>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="督办期限" prop="superviseperiod">
            <el-date-picker v-model="model.superviseperiod" type="date" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="督办原因" prop="supervisereason">
            <el-input v-model="model.supervisereason" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="督办响应" prop="superviseresponse">
            <el-input v-model="model.superviseresponse" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="督办发起部门" prop="supervisedeptname">
            <el-input v-model="model.supervisedeptname" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="督办发起人" prop="supervisepeople">
            <el-input v-model="model.supervisepeople" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="督办状态" prop="supervisestate">
            <el-select v-model="model.supervisestate">
              <el-option v-for="item in codeModel.supervisestateList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse>
        <el-collapse-item title="【单据】申请信息">
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请部门" prop="applydeptname">
                <tl-ref-dept v-model="model.applydeptcode" :initDisplayData.sync="model.applydeptname" :disabled="true" dataType="code" nameType="fullname" :singleSelect="true" :externalQueryParam="{qry_enabled:1}"></tl-ref-dept>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请公司" prop="applyorgname">
                <el-input v-model="model.applyorgname" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="申请人姓名" prop="applyusername">
                <tl-ref-user v-model="model.applyusername" :initDisplayData.sync="model.applyusername" :disabled="true" dataType="code" :singleSelect="true"></tl-ref-user>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="申请人工号" prop="applyusercode">
                <el-input v-model="model.applyusercode" :readonly="true" class="is-disabled"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单据日期" prop="docdate">
                <el-date-picker v-model="model.docdate" type="date" :disabled="true" placeholder="单据日期" value-format="yyyy-MM-dd"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提交时间" prop="submitdate">
                <el-date-picker v-model="model.submitdate" type="date" placeholder="提交时间" value-format="yyyy-MM-dd" disabled></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </el-form>
    </div>
    <tl-approval-history ref="history" sourceType="MpmProjectSupervise" :sourceId="model.id" />
    <div slot="footer" class="dialog-footer">
      <tl-attachment sourceType="MpmProjectSupervise" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
      <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory">
        <svg-icon icon-class="list" />审批历史
      </el-button>
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: {},
  data() {
    return {
      moduleTitle: '项目督办',
      urlPrefix: '/mpm/projectSupervise/',
      milepostvisible: true,
      taskvisible: false,
      externalQueryParamForTask: {},
      externalQueryParamForMilepost: {},
      meetvisible: false,
      bidsourcevisible: false,
      taskvisible2: false,
      projectVisible: false,
      // 进度对应编码
      progressArray: ['0', '3', '4', '5', '1', '2', '6', '7', '8', '9'],
      qualityArray: ['0', '10', '11', '12', '13', '14', '15', '16', '17'],
      // 赋值项目名称和项目经理的大类
      classProject: ['3', '4', '5', '1', '9', '10', '11', '12', '17'],
      // 里程碑处理
      classMilepost: ['2', '13'],
      // 任务处理
      classTask: ['6', '14'],
      // 自评处理
      classBidSource: ['7', '15'],
      // 会议处理
      classMeet: ['8', '16'],
      url: {
        // 额外的url
      }
    }
  },
  props: {
    projectinfo: {}
  },
  computed: {
    dodisabled() {
      return (this.isAdd === false && this.disableEditSubmit === true) || this.fromApproval === '1'
    },
    computedBigtype() {
      if (!this.model.supervisetype) {
        return this.codeModel.superviseobjecttypeList
      } else if (this.model.supervisetype === '0') {
        // 进度对应
        return this.codeModel.superviseobjecttypeList.filter((item) => this.progressArray.includes(item.code))
      } else {
        // 质量对应
        return this.codeModel.superviseobjecttypeList.filter((item) => this.qualityArray.includes(item.code))
      }
    }
  },
  methods: {
    changesupervisetype() {
      // 清除垃圾数据
      this.$set(this.model, 'superviseobjecttype', '')
      this.$set(this.model, 'superviseobject', '')
      this.$set(this.model, 'superviseobjectcode', '')
      this.$set(this.model, 'responsiblecode', '')
      this.$set(this.model, 'responsiblename', '')
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    changesuperviseobjecttype(e) {
      // 清除垃圾数据
      this.$set(this.model, 'superviseobject', '')
      this.$set(this.model, 'superviseobjectcode', '')
      this.$set(this.model, 'responsiblecode', '')
      this.$set(this.model, 'responsiblename', '')
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      // 其他
      if (e === '0') {
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = true
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = false
        this.$set(this.model, 'superviseobjectcode', this.projectinfo.projectcode)
      } else if (this.classMilepost.includes(e)) {
        // 里程碑及任务
        this.projectVisible = false
        this.milepostvisible = true
        this.taskvisible = false
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = false
      } else if (this.classProject.includes(e)) {
        // 项目等
        this.model.superviseobject = this.model.projectname
        this.milepostvisible = false
        this.taskvisible = false
        this.projectVisible = true
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = false
        // 责任人赋值
        this.$set(this.model, 'responsiblecode', this.projectinfo.projectmanagercode)
        this.$set(this.model, 'responsiblename', this.projectinfo.projectmanager)
      } else if (this.classTask.includes(e)) {
        // 任务
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = false
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = true
      } else if (this.classBidSource.includes(e)) {
        // 自评
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = false
        this.meetvisible = false
        this.bidsourcevisible = true
        this.taskvisible2 = false
        this.$set(this.model, 'responsiblecode', this.projectinfo.projectmanagercode)
        this.$set(this.model, 'responsiblename', this.projectinfo.projectmanager)
      } else if (this.classMeet.includes(e)) {
        // 会议
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = false
        this.meetvisible = true
        this.bidsourcevisible = false
        this.taskvisible2 = false
        this.$set(this.model, 'responsiblecode', this.projectinfo.projectmanagercode)
        this.$set(this.model, 'responsiblename', this.projectinfo.projectmanager)
      }
    },
    /** 获取里程碑及任务参照数据 */
    getMpmProjectMilepostData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'superviseobject', selectionRow.milepostname)
      this.$set(this.model, 'superviseobjectcode', selectionRow.milepostcode)
      // 里程碑及任务赋值责任人
      this.$set(this.model, 'responsiblecode', selectionRow.responsiblecode)
      this.$set(this.model, 'responsiblename', selectionRow.responsible)
    },
    /** 获取任务参照数据 */
    getMpmProjectTaskData(selectionRow, selectKey, selectionValue) {
      this.$set(this.model, 'superviseobject', selectionRow.taskname)
      this.$set(this.model, 'superviseobjectcode', selectionRow.taskcode)
      // 赋值任务责任人
      this.$set(this.model, 'responsiblecode', selectionRow.responsiblecode)
      this.$set(this.model, 'responsiblename', selectionRow.responsible)
    },
    // 督办部门从一级部门显示
    onelevelDeptname(deptName) {
      if (!deptName) {
        return
      }
      if (deptName.indexOf('鼎信股份') !== -1) {
        let num
        num = deptName.indexOf('\\') + 1
        return deptName.substring(num)
      } else {
        return deptName
      }
    },
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        let a = this.projectinfo
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'disporgcode', this.org.name)
        this.$set(this.model, 'applyorgcode', this.org.code)
        this.$set(this.model, 'applyorgname', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'applydeptcode', this.dept.code)
        this.$set(this.model, 'applydeptname', this.dept.fullname)
        // 督办部门信息
        this.$set(this.model, 'supervisedeptcode', this.dept.code)
        this.$set(this.model, 'supervisedeptname', this.onelevelDeptname(this.dept.fullname))
        // 督办发起人信息
        this.$set(this.model, 'supervisepeoplecode', this.user.code)
        this.$set(this.model, 'supervisepeople', this.user.name)
        // 督办状态
        this.$set(this.model, 'supervisestate', '0')
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'applyusercode', this.user.code)
        this.$set(this.model, 'applyusername', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        // 督办期限为3天
        var date = new Date()
        date.setDate(date.getDate() + 2)
        this.$set(this.model, 'superviseperiod', date)
      }
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
        this.$set(this.model, 'supervisedeptname', this.onelevelDeptname(this.model.supervisedeptname))
      }
      let e = this.model.superviseobjecttype
       // 其他
       if (e === '0') {
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = true
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = false
      } else if (this.classMilepost.includes(e)) {
        // 里程碑及任务
        this.projectVisible = false
        this.milepostvisible = true
        this.taskvisible = false
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = false
      } else if (this.classProject.includes(e)) {
        // 项目等
        this.model.superviseobject = this.model.projectname
        this.milepostvisible = false
        this.taskvisible = false
        this.projectVisible = true
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = false
      } else if (this.classTask.includes(e)) {
        // 任务
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = false
        this.meetvisible = false
        this.bidsourcevisible = false
        this.taskvisible2 = true
      } else if (this.classBidSource.includes(e)) {
        // 自评
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = false
        this.meetvisible = false
        this.bidsourcevisible = true
        this.taskvisible2 = false
      } else if (this.classMeet.includes(e)) {
        // 会议
        this.projectVisible = false
        this.milepostvisible = false
        this.taskvisible = false
        this.meetvisible = true
        this.bidsourcevisible = false
        this.taskvisible2 = false
      }
      // 获取审批参数
      this.$nextTick((_) => {
        let approvalparam = this.approvalNodeModel.approvalparam
        let approvalparamobject = JSON.parse(approvalparam)
        this.extraParam = approvalparamobject ? approvalparamobject.extraParam : ''
        if (this.extraParam === 'Owner') {
          this.$nextTick(() => {
            this.$refs.dealform.clearValidate()
          })
        }
      })
    },
    userConfirm(selectionRow) {
      this.$set(this.model, 'responsiblecode', selectionRow[0].code)
      this.$set(this.model, 'responsiblename', selectionRow[0].name)
    },
    /** 编辑，发请求获得数据 */
    queryData(id) {
      if (!this.url.load) {
        this.$tap.alert('请设置load地址!', 'warning')
        return
      }
      return getAction(this.url.load, { id: id }).then((res) => {
        if (res.success) {
          this.model = res.result
          this.model.superviseobjecttype && !Array.isArray(this.model.superviseobjecttype) ? this.$set(this.model, 'superviseobjecttype', this.model.superviseobjecttype.split(',')) : null
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    }
  }
}
</script>

